using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using APNotifications.Dialog;

namespace APNotifications.Controls
{
    public partial class Settings : UserControl
    {
        public Settings()
        {
            InitializeComponent();
        }

        private void Settings_Load(object sender, EventArgs e)
        {
            BindControls();
        }

        private void BindControls()
        {
            cbNotifications.Checked = Config.ShowNotifications();
            cbSoundOn.Checked = Config.SoundOn();
            cbProxyAuthentication.Checked = Config.ManualProxy();

            if (cbProxyAuthentication.Checked)
            {
                txtUsername.Enabled = true;
                txtUsername.Text = Config.UserName();
                txtPassword.Enabled = true;
                txtPassword.Text = Config.Password();
                txtDomain.Enabled = true;
                txtDomain.Text = Config.Domain();
            }

            if (cbSoundOn.Checked)
            {
                btnSound.Enabled = true;
            }


            txtRefreshRate.Text = Convert.ToString((Config.RefreshRate() / 1000));
        }

        private void cbProxyAuthentication_CheckedChanged(object sender, EventArgs e)
        {
            if (cbProxyAuthentication.Checked)
            {
                txtUsername.Enabled = true;
                txtUsername.Text = Config.UserName();
                txtPassword.Enabled = true;
                txtPassword.Text = Config.Password();
                txtDomain.Enabled = true;
                txtDomain.Text = Config.Domain();
            }
            else
            {
                txtUsername.Enabled = false;
                txtPassword.Enabled = false;
                txtDomain.Enabled = false;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (int.Parse(txtRefreshRate.Text.Trim()) != 0)
                {
                    Config.SaveShowNotifications(cbNotifications.Checked);
                    Config.SaveSoundOn(cbSoundOn.Checked);
                    Config.SaveManualProxy(cbProxyAuthentication.Checked);
                    Config.SaveRefreshRate(int.Parse(txtRefreshRate.Text.Trim()) * 1000);                    

                    if (cbProxyAuthentication.Checked)
                    {
                        if (string.IsNullOrEmpty(txtUsername.Text) || string.IsNullOrEmpty(txtPassword.Text) || string.IsNullOrEmpty(txtDomain.Text))
                        {
                            MessageBox.Show("Username, password and domain required", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                        else
                        {
                            Config.SaveNetworkCredentials(txtUsername.Text.Trim(), txtPassword.Text.Trim(), txtDomain.Text.Trim());
                        }
                    }

                    this.FindForm().Close();
                }
                else
                {
                    MessageBox.Show("Refresh rate cannot be 0", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception)
            {                
                MessageBox.Show("Please enter a valid refresh rate", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);                
            }                        
            
        }

        private void cbSoundOn_CheckedChanged(object sender, EventArgs e)
        {
            if (cbSoundOn.Checked)
            {
                btnSound.Enabled = true;
            }
            else
            {
                btnSound.Enabled = false;
            }
        }

        private void btnSound_Click(object sender, EventArgs e)
        {
            frmSoundSettings _frmSoundSettings = new frmSoundSettings();
            _frmSoundSettings.Show();
        }
    }
}
